package com.caifeng.contentproviderwork;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.Random;

/**
 * 内容提供者
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn, delelte, update, query;
    ContentResolver contentResolver;
    Uri uri;
    ContentValues contentValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        uri = Uri.parse("cotent://com.caifeng.contentproviderwork/info");
        getContentResolver().registerContentObserver(uri, true, new My(new Handler()));

    }

    class My extends ContentObserver {
        public My(Handler handler) {
            super(handler);

        }

        @Override
        public void onChange(boolean selfChange) {
            Log.e("我是监视器", "有人在偷看你的数据库");
            super.onChange(selfChange);
        }

    }

    private void initView() {
        btn = findViewById(R.id.btn);
        delelte = findViewById(R.id.btn_delete);
        update = findViewById(R.id.btn_update);
        query = findViewById(R.id.btn_select);
        btn.setOnClickListener(this);
        delelte.setOnClickListener(this);
        update.setOnClickListener(this);
        query.setOnClickListener(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getContentResolver().unregisterContentObserver(new My(new Handler()));
    }

    private void createDB() {
        PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        for (int i = 0; i < 3; i++) {
            contentValues = new ContentValues();
            contentValues.put("name", "cf" + i);
            db.insert("info", null, contentValues);
        }
        db.close();
    }

    @Override
    public void onClick(View v) {
        contentResolver = getContentResolver();
        uri = Uri.parse("content://com.caifeng.contentproviderwork/info");
        contentValues = new ContentValues();
        switch (v.getId()) {
            case R.id.btn:

                Random random = new Random();
                contentValues.put("name", "add_itcast" + random.nextInt(100));
                Uri uri1 = contentResolver.insert(uri, contentValues);
                Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
                Log.e("name", "success");
                break;
            case R.id.btn_delete:
                int d = contentResolver.delete(uri, "name=?", new String[]{"itcast0"});
                Toast.makeText(this, "成功" + d + "行", Toast.LENGTH_SHORT).show();
                Log.e("删除", "成功");

                break;
            case R.id.btn_update:
                contentValues.put("name", "update_itcast");
                int upDateCount = contentResolver.update(uri, contentValues, "name=?", new String[]{"itcast1"});
                Toast.makeText(this, "" + upDateCount, Toast.LENGTH_SHORT).show();

                break;
            case R.id.btn_select:

                break;

        }
    }
}
